package application.gui.main.startWindow;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import application.LoginAndAdministrationModule.Logging;
import application.LoginAndAdministrationModule.PersonData;
import application.gui.adminWindow.AdminHomePage;
import application.gui.clientWindow.ClientHomePage;
import application.gui.workerWindow.WorkerHomePage;

public class LoginPanel extends JPanel {

	public LoginPanel(Homepage parent){
		this.parent = parent;
		createAndShowLoginPanel();
	}
	
	private void createAndShowLoginPanel() {

		setSize(600, 360);
		setLayout(null);
		setVisible(false);
		loginInput = new JTextField(25);
		passInput = new JPasswordField(25);
		loginEnter = new JButton("Zaloguj");
		errorInfo = new JLabel();
		addComponent(loginInput, 225, 120, 150, 25);
		addComponent(passInput, 225, 160, 150, 25);
		addComponent(loginEnter, 250, 200, 100, 25);
		addComponent(errorInfo, 150, 80, 300, 30);
		login = new JLabel("Login");
		addComponent(login, 175, 120, 50, 25);
		pass = new JLabel("Hasło");
		addComponent(pass, 175, 160, 50, 25);
		errorInfo.setVisible(false);
		
		loginEnter.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				Logging loguj = new Logging();
				try {
					if (loguj.logIn(loginInput.getText(),
							String.valueOf(passInput.getPassword()))) {
						errorInfo.setVisible(false);
						loggedUser = loguj.getLoggedInUser();
						switch(loggedUser.getRights()){
						case 1:
							EventQueue.invokeLater(new Runnable() {
								public void run() {
									ClientHomePage page = new ClientHomePage(
											loggedUser, parent);
									page.setVisible(true);
									parent.setVisible(false);
								}
							});
							break;
						case 2:
							EventQueue.invokeLater(new Runnable() {
								public void run() {
									WorkerHomePage page = new WorkerHomePage(
											loggedUser, parent);
									page.setVisible(true);
									parent.setVisible(false);
								}
							});
							break;
						case 3:
							EventQueue.invokeLater(new Runnable() {
								public void run() {
									AdminHomePage page = new AdminHomePage(
											loggedUser, parent);
									page.setVisible(true);
									parent.setVisible(false);
								}
							});
							break;
						}
					}else{
						errorInfo.setVisible(true);
						errorInfo.setText("Błędny login lub hasło");
					}

				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					errorInfo.setVisible(true);
					errorInfo.setText("Nieudane logowanie");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					errorInfo.setVisible(true);
					errorInfo.setText("Nieudane logowanie");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					errorInfo.setVisible(true);
					errorInfo.setText("Nieudane logowanie");
				}

			}
		});
		
	}

	private void addComponent(JComponent comp, int x, int y, int width,
			int height) {
		add(comp);
		comp.setBounds(x, y, width, height);
	}
	
	

	

	public void setErrorInfoText(String text) {
		this.errorInfo.setText(text);
		errorInfo.setVisible(true);
	}

	public void clearFields(){
		loginInput.setText("");
		passInput.setText("");
		errorInfo.setText("");
	}

	private JLabel login;
	private JLabel pass;
	private JButton loginEnter;
	private JTextField loginInput;
	private JPasswordField passInput;
	private JLabel errorInfo;
	
	private Homepage parent;
	
	/**
	 * Dane użytkownika
	 */
	private PersonData loggedUser = null;
}
